<div class="content-wrapper">
    <!-- Content Header (Page header) -->
    <section class="content-header">
        <h1>
            管理员列表
            <small>it all starts here</small>
        </h1>
        <ol class="breadcrumb">
            <li><a href="{:url('/admin/index/index')}"><i class="fa fa-dashboard"></i> 控制台</a></li>
            <li><a href="{:url('/admin/user/admin')}">管理员管理</a></li>
        </ol>
    </section>

    <!-- Main content -->
    <section class="content">
        <div class="box">
            <div class="box-header with-border">
                <h3 class="box-title">管理员列表</h3>
                <btton class="btn btn-primary pull-right" onclick="addAdmin(this,0)"><i class="fa fa-plus"></i> 添加管理员</btton>
            </div>
            <!-- /.box-header -->
            <div class="box-body">
                <table class="table table-bordered center"><tbody>
                <tr>
                    <th style="width: 60px">
                        <label for="checkall" class="btn btn-default">
                            <input type="checkbox" value="" id="checkall" class="pointer">
                            全选
                        </label>
                    </th>
                    <th>管理员</th>
                    <th style="width: 100px;text-align: center;">角色</th>
                    <th>邮箱</th>
                    <th>加入时间</th>
                    <th>最后登录时间</th>
                    <th>最后登录IP</th>
                    <th style="width: 100px;text-align: center">操作</th>
                </tr>

                <?php foreach($admin as $item){?>
                <tr>
                    <td>
                        <label class="lb">
                            <input type="checkbox" class="cbox"  value="" name="idarr[]">
                        </label>
                    </td>
                    <td><?php echo $item['username']; ?></td>
                    <td><?php echo $item['name']; ?></td>
                    <td><?php echo $item['email']; ?></td>
                    <td><?php echo date('Y-m-d H:i',$item['add_time']); ?></td>
                    <td><?php echo date('Y-m-d H:i',$item['last_login']); ?></td>
                    <td><?php echo $item['last_ip']; ?></td>
                    <td>
                        <a href="#" class="btn btn-info btn-sm" onclick="addAdmin(this,<?php echo $item['ad_id']?>)"><i class="fa fa-edit"></i></a>
                        <a href="#" class="btn btn-danger btn-sm"><i class="fa fa-trash-o"></i></a>
                    </td>
                </tr>
                <?php } ?>
                </tbody></table>
            </div>
            <!-- /.box-body -->
            <div class="box-footer clearfix">
                <button type="button" class="btn btn-danger"><i class="fa fa-trash-o"></i> 删除</button>
                {$admin|raw}
            </div>
        </div>
    </section>
    <!--模态框-->
    <div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" id="exampleModalLabel">New message</h4>
                </div>
                <div class="modal-body">
                    <form class="form-horizontal" id="adminForm">
                        <input type="hidden" name="ad_id" id="hidden">
                        <div class="form-group">
                            <label for="inputname" class="col-sm-2 control-label">用户名</label>
                            <div class="col-sm-10">
                                <input type="text" name="username" class="form-control" id="inputname" placeholder="Jane Doe">
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="inputPassword" class="col-sm-2 control-label">密 码</label>
                            <div class="col-sm-10">
                                <input type="password" name="password" class="form-control" id="inputPassword" placeholder="Password">
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="selectRole" class="col-sm-2 control-label">角色</label>
                            <div class="col-sm-10">
                            <select id="selectRole" class="form-control" name="r_id" width="100%">
                                <?php foreach($role as $item){ ?>
                                <option value="<?php echo $item['r_id']; ?>"><?php echo $item['name']; ?></option>
                                <?php } ?>
                            </select>
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="col-sm-12">
                                <button type="button" onclick="update()" class="btn btn-success pull-right"> 保 存 </button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
    <!--小型模态框-->
    <div class="modal fade bs-example-modal-sm" id="smallModal" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel" style="display: none;">
        <div class="modal-dialog modal-sm" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
                    <h4 class="modal-title" id="mySmallModalLabel">Small modal</h4>
                </div>
            </div><!-- /.modal-content -->
        </div><!-- /.modal-dialog -->
    </div>
</div>
<script type="text/javascript">
    //触发模态框的同时调用此方法
    function addAdmin(obj,type){
        if (type==0){
            $('#exampleModalLabel').text('添加管理员');
            $('#hidden').val(0);
            $('#inputPassword').attr('placeholder','6~24位密码');
        }else{
            $('#exampleModalLabel').text('编辑管理员');
            $('#hidden').val(type);
            var tds= $(obj).parent().find('td');
            $('#inputname').val($(obj).parent().siblings().eq(1).text());
            $('#inputPassword').attr('placeholder','不填则不修改');
        }

        $('#exampleModal').modal('show');
    }
    //提交更改
    function update() {
        //获取模态框数据
        var data = $('#adminForm').serializeArray();
        $.ajax({
            type: "post",
            url: "{:url('/admin/user/adminEdit')}",
            data: data,
            dataType: 'json',
            contentType: "application/x-www-form-urlencoded; charset=utf-8",
            success: function(result) {
                $('#exampleModal').modal('hide');
                if (parseInt(result)){
                    // 保存成功
                    $('#mySmallModalLabel').text('保存成功!');
                    $('#smallModal').modal('show');
                } else{
                    //保存失败
                    $('#mySmallModalLabel').text('保存失败!');
                    $('#smallModal').modal('show');
                }
                setTimeout(function(){
                    $('#smallModal').modal('hide');
                    window.location = "{:url('/admin/user/admin')}";
                },3000);
            }
        });
    }

</script>